from django.shortcuts import render
from django.http import JsonResponse
from .models import Teacher, Position
from classapp.models import College, Major
from courseapp.models import Course
from rest_framework.parsers import JSONParser
from rest_framework.decorators import api_view
from UserApp.models import User
from django.core.paginator import Paginator
from university_management_system import utils


# Create your views here.

def teacher_list(request):
    teacher_id = request.GET.get('teacher_id')
    name = request.GET.get('name')
    id_card = request.GET.get("id_card")

    where = {}
    if not utils.is_empty(teacher_id):
        where['teacher_id__contains'] = teacher_id
    if not utils.is_empty(name):
        where['name__contains'] = name
    if not utils.is_empty(id_card):
        where['id_card__contains'] = id_card
    teacherobj = Teacher.objects.filter(**where).values()

    teacher_list = []
    for row in teacherobj:
        print(row)
        colleage = College.objects.filter(id=row['college_id']).values().first()
        print(colleage)
        course = Course.objects.filter(course_id=row['college_id']).values().first()
        print(course)
        position = Position.objects.filter(id=row['position']).values('name').first()
        row['position'] = position
        row['college_id'] = {
            "name": colleage['name'],
            "course": course['name']
        }
        teacher_list.append(row)
    return JsonResponse({"code": 200, "message": "查询成功", "data": teacher_list})


def add_teacher(request):
    data = JSONParser().parse(request)
    Teacher.objects.create(**data)
    return JsonResponse({"code": 200, "message": "创建成功"})


def get_college(request):
    data = College.objects.filter().values()
    print(data)
    college_list = []
    for row in data:
        college_list.append(row)
    return JsonResponse({"code": 200, "message": "查询成功", "data": college_list})


def get_postion(request):
    data = Position.objects.filter().values()
    print(data)
    postion_list = []
    for row in data:
        postion_list.append(row)
    return JsonResponse({"code": 200, "message": "查询成功", "data": postion_list})


def delete_teacher(request, teacher_id):
    if utils.is_empty(teacher_id):
        return JsonResponse({"code": 400, "message": "教师id不能为空"})
    elif Teacher.objects.filter(teacher_id=teacher_id).exists():
        Teacher.objects.filter(teacher_id=teacher_id).delete()
        return JsonResponse({"code": 200, "message": "删除成功"})
    else:
        return JsonResponse({"code": 200, "message": "教师id不存在"})


def update_teacher(request, teacher_id):
    data = JSONParser().parse(request)
    if utils.is_empty(teacher_id):
        return JsonResponse({"code": 400, "message": "教师id不能为空"})
    elif Teacher.objects.filter(teacher_id=teacher_id).exists():
        Teacher.objects.filter(teacher_id=teacher_id).update(**data)
        return JsonResponse({"code": 200, "message": "修改成功"})
    else:
        return JsonResponse({"code": 404, "message": "教师id不存在"})


def get_teacher(request, teacher_id):
    data = Teacher.objects.filter(teacher_id=teacher_id).values().first()
    return JsonResponse({"code": 200, "message": "查询成功", "data": data})
